;(function() {
	Nokia.TwitterItemView = Nokia.View.extend({
		
		/*
		 * Initializes Guarana components
		 */
		init: function()
		{
			var instance = this;
			
			this.contentElement = this.getContainer().find('#twitter-item-content');
			
			this.newsOpenButton = new Nokia.Button({
				element: '#twitter-item-open-button',
				label: 'User Profile',
				width: '80%',
				click: function() {
					instance.openInWebBrowser();
				}
			});
		},
		
		/*
		 * Opens the Web page of the user's Twitter profile
		 */
		openInWebBrowser: function()
		{
			widget.openURL('http://twitter.com/' + this.item.from_user);
		},
		
		/*
		 * Shows a Twitter result item
		 */
		showItem: function(item)
		{
			this.item = item;

			var itemHTML = Nokia.GBCServices.getServiceItemSnippet(item);
				
			// Create a Button for each link contained in the Twitter message;
			
			var links = item.text.match(/(http[^ ]+)/g);
			
			if(links)
			{
				for(var i = 0; i < links.length; i++)
				{
					// Create the Button HTML code
					
					itemHTML += '<div id="twitter-item-link-' + i + '" class="ui-helper-clearfix"></div>';
				}
			}

			this.contentElement.html(itemHTML);
			
			if(links)
			{
				for(var i = 0; i < links.length; i++)
				{
					// Create the Guarana Button component
					
					this.newsOpenButton = new Nokia.Button({
						element: '#twitter-item-link-' + i,
						label: links[i],
						url: links[i],
						width: '80%',
						click: function() {
							widget.openURL(this.options.url);
						}
					});
				}
			}
			
			new Nokia.TextLabel({
				element: '#twitter-item-text',
				content: '#twitter-item-text-content'
			});
		},
		
		/*
		 * Called when the view needs to be shown
		 */
		show: function(item)
		{
			// Replace the Exit button with a Back button
			
			var template = this.getTemplate();
			
			var topBar = template.getHeaderTopBar();
			var viewManager = template.getViewManager();
			
			var defaultTopItems = GBC.getDefaultTopItems();
			defaultTopItems[0] = {classname: 'nokia-template-header-icon-back', callback: function() { viewManager.back() }};
			topBar.setItems(defaultTopItems);
			
			this.showItem(item);
			
			this.getContainer().show();
		}
	});
})();